package everydayGrade.t6;

/**
 * @Author: Siantar
 * @Date: 2023-07-20-9:47
 * @Description: 1.0
 */
public class T0918 {
    public int maxSubarraySumCircular(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        int res = Math.max(nums[0],sum);
        int maxLeft = nums[0], minLeft = nums[0], leftSum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            leftSum+=nums[i];
            res = Math.max(res, Math.max(leftSum - minLeft, sum - (leftSum - maxLeft)));
            if (leftSum > maxLeft) {
                maxLeft = leftSum;
            }
            if (leftSum < minLeft) {
                minLeft = leftSum;
            }
        }
        return res;
    }
}
